package com.myjob.handlerImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.myjob.common.DubboConfig;
import com.myjob.config.MjServiceName;
import com.myjob.dto.ProfileDto;
import com.myjob.entity.*;
import com.myjob.handler.ProfileHandler;
import com.myjob.mapper.*;
import com.myjob.service.BaseInfoService;
import com.myjob.utils.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Chen Yuan
 * @create 2021-01-28  10:49
 */
@Slf4j
@Component
@Service(group = MjServiceName.JOB,
		retries = DubboConfig.RETRIES,
		timeout = DubboConfig.TIME_OUT,
		version = DubboConfig.VERSION)
public class ProfileHandlerImpl implements ProfileHandler {
	@Autowired
	private BaseInfoService baseInfoService;

	@Autowired
	private EducationMapper educationMapper;

	@Autowired
	private SkillMapper skillMapper;

	@Autowired
	private ProjectMapper projectMapper;

	@Autowired
	private WebsiteMapper websiteMapper;

	@Autowired
	private WorkMapper workMapper;

	@Override
	public ProfileDto selectProfileByUserId(String userId) {
		log.info("[Dubbo] - 被远程调用 -> ProfileHandlerImpl -> selectProfileByUserId("+userId+")");

		if (UtilValidate.isEmpty(userId)){
			return null;
		}

		ProfileDto profileDto = new ProfileDto();

		// 基本信息
		BaseInfo baseInfo = (BaseInfo) baseInfoService.selectBaseInfoById(userId).getDatas();
		profileDto.setBaseInfo(baseInfo);

		// 教育经历
		QueryWrapper<Education> wrapper2 = new QueryWrapper<>();
		wrapper2.eq("user_id", userId);
		List<Education> educations = educationMapper.selectList(wrapper2);
		profileDto.setEducations(educations);

		// 项目经验
		QueryWrapper<Project> wrapper3 = new QueryWrapper<>();
		wrapper3.eq("user_id", userId);
		List<Project> projects = projectMapper.selectList(wrapper3);
		profileDto.setProjects(projects);

		// 技能信息
		QueryWrapper<Skill> wrapper4 = new QueryWrapper<>();
		wrapper4.eq("user_id", userId);
		List<Skill> skills = skillMapper.selectList(wrapper4);
		profileDto.setSkills(skills);

		// 个人网站
		QueryWrapper<Website> wrapper5 = new QueryWrapper<>();
		wrapper5.eq("user_id", userId);
		List<Website> websites = websiteMapper.selectList(wrapper5);
		profileDto.setWebsites(websites);

		// 工作经验
		QueryWrapper<Work> wrapper = new QueryWrapper<>();
		wrapper.eq("user_id", userId);
		List<Work> works = workMapper.selectList(wrapper);
		profileDto.setWorks(works);

		return profileDto;
	}
}
